package com.hudilao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hudilao.common.R;
import com.hudilao.entity.Employee;
import com.hudilao.mapper.EmployeeMapper;
import com.hudilao.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

@Service
@Transactional
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;


    //管理员注册（功能暂时不用）
    @Override
    public R<Employee> registerEmployee(Employee employee) {
        //密码加密
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

        //加密后设置密码
        employee.setPassword(password);

        //构建username员工查询
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee existingEmployee = this.getOne(queryWrapper);

        //如果数据库中查到了
        if (existingEmployee != null) {
            //用户已存在登录失败
            return R.error("用户名已存在，注册失败");
        }

        //注册成功则保存信息
        boolean saveResult = this.save(employee);

        if (saveResult) {
            return R.success(employee);
        } else {
            return R.error("注册失败，请重试");
        }

    }


    //管理端：新增员工
    @Override
    public R<String> saveEmployee(Employee employee) {
        //设置密码后加密
        String rawPassword = employee.getPassword();

        //设置密码条件-长度至少8位
        if (rawPassword == null || rawPassword.length() < 8) {
            return R.error("密码长度必须至少为8位");
        }

        employee.setPassword(DigestUtils.md5DigestAsHex(rawPassword.getBytes()));

        //保存员工信息
        this.save(employee);

        //根据role判断新增的是员工还是经理
        if (employee.getRole() == 0) {
            return R.success("新增经理成功");
        } else {
            return R.success("新增员工成功");
        }

    }


    //管理端：员工信息分页查询
    @Override
    public R<Page> pageEmployee(int page, int pageSize, String username) {

        //构建分页page
        Page<Employee> pageInfo = new Page<>(page, pageSize);

        //构建查询
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();

        //根据username查询
        if (username != null) {
            queryWrapper.like(Employee::getUsername, username);
        }

        //设置排序条件-根据创建时间降序排列
        queryWrapper.orderByDesc(Employee::getCreateTime);

        this.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }


    //管理端：更新员工状态-改变启用禁用
    @Override
    public R<String> updateEmployeeStatus(Employee employee) {

        //构建查询
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();

        //根据username查询
        queryWrapper.eq(Employee::getId, employee.getId());
        Employee currentEmployee = this.getOne(queryWrapper);

        //员工不存在
        if (currentEmployee == null) {
            return R.error("员工不存在");
        }

        //员工存在：改变员工状态相反
        int status;
        if (employee.getStatus() == 0) {
            status = 1;
        } else {
            status = 0;
        }


        //设置状态
        currentEmployee.setStatus(status);

        //获取更新结果
        boolean result = this.updateById(currentEmployee);

        if (result) {
            return R.success("员工状态更新成功");
        } else {
            return R.error("员工状态更新失败");
        }

    }


    //管理端：修改员工信息
    @Override
    public R<String> updateEmployee(Employee employee) {

        //根据id修改员工信息
        this.updateById(employee);

        return R.success("员工信息修改成功");
    }


    //管理端：通过id查询员工
    /**
     * 根据用户名查询员工信息
     * @param id 用户id
     * @return R<Employee> 员工信息响应
     */
    public R<Employee> getEmployeeById(Long id) {

        // 根据id查询员工信息
        Employee employee = employeeMapper.getEmployeeById(id);

        // 判断员工是否存在
        if (employee != null) {
            return R.success(employee);
        } else {
            return R.error("没有查询到对应员工信息");
        }
    }


    //管理端：删除员工
    @Override
    public R<String> deleteEmployee(Long id) {

        //新建查询
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();

        //根据username查询
        queryWrapper.eq(Employee::getId, id);
        Employee employee = this.getOne(queryWrapper);

        if (employee == null) {
            return R.error("员工不存在，删除失败");
        }

        if (employee.getStatus() == 1) {
            return R.error("员工处于启用状态，不能删除");
        }

        //判断删除结果
        boolean result = this.remove(queryWrapper);
        if (result) {
            return R.success("员工删除成功");
        } else {
            return R.error("删除失败，请重试");
        }

    }
}

